#include "gCamera.h"
#include "config.h"


#include <math.h>

CCamera::CCamera()
:m_camPosX(0),
m_camPosY(0),
m_camPosZ(0),
fov(90),
view_dist(1),
viewport_width(SCREEN_WIDTH),
viewport_height(SCREEN_HEIGHT),
near_clip_z(50),
far_clip_z(500),
m_needRefresh(true)
{
	assert(view_dist < near_clip_z);

	m_mathTool.GetSinCos();

	aspect_ratio = (float)viewport_width/(float)viewport_height;
	viewport_center_x = (viewport_width-1)/2;
	viewport_center_y = (viewport_height-1)/2;
	viewplane_width = 2; 
	viewplane_height = 2;
}

CCamera::~CCamera()
{
}

//void CCamera::SetCam(float _fov, float _view_dist, float _viewport_width, float _viewport_height, 
//					 float near_z, float far_z)
//{
//	assert(view_dist < near_clip_z);
//
//	fov = _fov;
//	view_dist = _view_dist;
//	viewport_width = _viewport_width;
//	viewport_height = _viewport_height;
//	near_clip_z = near_z;
//	far_clip_z = far_z;
//	m_needRefresh = true;
//
//	aspect_ratio = viewport_width/viewport_height;
//	viewport_center_x = (viewport_width-1)/2;
//	viewport_center_y = (viewport_height-1)/2;
//	viewplane_width = view_dist*tan(fov/2)*2; 
//	viewplane_height = viewplane_width/aspect_ratio;
//}

void CCamera::Build_toPer_Matrix()
{
	toPer.LoadIdentity();
	toPer.SetElement(1, 2, 3);
	toPer.SetElement(0, 3, 3);
}

//void CCamera::Build_toScr_Matrix()
//{
//}

//---------------ELUER CAMERA------------------
CCameraELUER::CCameraELUER()
{
	rx = 0;
	ry = 0;
	rz = 0;
	//viewVec.Set(0, 0, 1);
	Build_toPer_Matrix();
}

CCameraELUER::~CCameraELUER()
{
}

void CCameraELUER::Build_toCam_Matrix()
{
	CGLIBMatrix tmpX;
	CGLIBMatrix tmpY;
	CGLIBMatrix tmpZ;
	CGLIBMatrix tmpTranslate;

	toCam.LoadIdentity();

	tmpTranslate.LoadIdentity();
	tmpTranslate.SetElement(-(m_camPosX*1.0), 3, 0);
	tmpTranslate.SetElement(-(m_camPosY*1.0), 3, 1);
	tmpTranslate.SetElement(-(m_camPosZ*1.0), 3, 2);
	toCam.MultMatrix(tmpTranslate);

	tmpX.LoadIdentity();
	tmpX.SetElement(m_mathTool.myCos[rx], 1, 1);
	tmpX.SetElement(-m_mathTool.mySin[rx], 1, 2);
	tmpX.SetElement(m_mathTool.mySin[rx], 2, 1);
	tmpX.SetElement(m_mathTool.myCos[rx], 2, 2);
	toCam.MultMatrix(tmpX);

	tmpY.LoadIdentity();
	tmpY.SetElement(m_mathTool.myCos[ry], 0, 0);
	tmpY.SetElement(m_mathTool.mySin[ry], 0, 2);
	tmpY.SetElement(-m_mathTool.mySin[ry], 2, 0);
	tmpY.SetElement(m_mathTool.myCos[ry], 2, 2);
	toCam.MultMatrix(tmpY);

	tmpZ.LoadIdentity();
	tmpZ.SetElement(m_mathTool.myCos[rz], 0, 0);
	tmpZ.SetElement(-m_mathTool.mySin[rz], 0, 1);
	tmpZ.SetElement(m_mathTool.mySin[rz], 1, 0);
	tmpZ.SetElement(m_mathTool.myCos[rz], 1, 1);
	toCam.MultMatrix(tmpZ);
}

void CCameraELUER::Update()
{
	Build_toCam_Matrix();
	//BuildViewVector();
}

//----------------UVN CAMERA-----------------------
CCameraUVN::CCameraUVN()
{
}

CCameraUVN::~CCameraUVN()
{
}

void CCameraUVN::Build_toCam_Matrix()
{
}

void CCameraUVN::Update()
{
}